﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Vra.DataAccess.Entities;
using System.Windows.Forms;

namespace Vra.DataAccess
{
    public class InterestDao : IInterestsDao
    {
        /// <summary>
        /// Возвращает строку подключения к базе
        /// </summary>
        /// <returns></returns>
        private static string GetConnectionString()
        {
            return ConfigurationManager.ConnectionStrings["vradb"].ConnectionString;
        }

        /// <summary>
        /// Возвращает объект подключения к базе
        /// </summary>
        /// <returns></returns>
        private static SqlConnection GetConnection()
        {
            return new SqlConnection(GetConnectionString());
        }

        private static Interests LoadInterest(SqlDataReader reader)
        {
            Interests interests = new Interests();

            interests.Artists = reader.GetString(reader.GetOrdinal("Artist"));
            interests.Customer = reader.GetString(reader.GetOrdinal("Customer"));

            return interests;
        }

        public IList<Interests> GetAll()
        {
            IList<Interests> interests = new List<Interests>();
            using (var conn = GetConnection())
            {
                conn.Open();
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT Artist, Customer FROM CustomerInterests";
                    using (var dataReader = cmd.ExecuteReader())
                    {
                        while (dataReader.Read())
                        {
                            interests.Add(LoadInterest(dataReader));
                        }
                    }
                }
            }

            return interests;
        }
    }
}
